package com.ktgj.hotel.utils;

import com.alibaba.fastjson.JSONObject;
import com.ktgj.common.core.utils.HttpClientUtil;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public class GaoDeUtil {

    //private final String GaoDeKey = "87378a4dc649aa04e01267b4fb56b091";  //吕英彪
    //private final String GaoDeKey = "282fc9c6fd0381c565ff8c40d1753bbb";  //初艳东
    //private final String GaoDeKey = "1613a231cdab0a87564cfc4747ead2b1";  //杨正冉
    private final String GaoDeKey = "ba584cc7cbc1494d9491e37f7b49b920";  //金色世纪

    /**
     * 根据经纬度获取所在城市(高德)
     * */
    public String getCityByCoordinate(String coordinate) {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + GaoDeKey + "&location=" + coordinate;
        String jsonStr = HttpClientUtil.doGet(url);
        JSONObject data = JSONObject.parseObject(jsonStr);
        if (data.getString("status").equals("0"))
            return "error";
        JSONObject regeocode = data.getJSONObject("regeocode");
        JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
        if (addressComponent.getString("city").equals("") || addressComponent.getString("city").equals("[]"))
            return addressComponent.getString("province");
        return addressComponent.getString("city");
    }

    /**
     * 根据经纬度获取所在城市(高德)调用方法
     * */
    public String cityName(String lng, String lat){
        //拼接经纬度
        String lngAndLat=lng+","+lat;

        //调用方法查询城市名称
        GaoDeUtil deUtil = new GaoDeUtil();
        String cityName=deUtil.getCityByCoordinate(lngAndLat);
        return cityName;
    }

    /**
     * 计算两坐标距离(高德)
     * 结果单位：米
     * */
    public double getDistanceMeter(String gpsFromCoordinate, String gpsToCoordinate)
    {
        String[] gpsFromArr = gpsFromCoordinate.split(",");
        String[] gpsToArr = gpsToCoordinate.split(",");
        GlobalCoordinates gpsFrom = new GlobalCoordinates(new Double(gpsFromArr[1]).doubleValue(), new Double(gpsFromArr[0]).doubleValue());
        GlobalCoordinates gpTo = new GlobalCoordinates(new Double(gpsToArr[1]).doubleValue(), new Double(gpsToArr[0]).doubleValue());

        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, gpsFrom, gpTo);

        return geoCurve.getEllipsoidalDistance();
    }

    /**
     * 计算两坐标距离(高德)调用方法
     * */
    public Double distanceMeter(String lng, String lat, String strLng, String strLat){
        //经纬度保留小数点后六位
        if (lng.length() >8){
            lng = lng.substring(0, 8);
        }
        else if (lat.length() > 8){
            lat = lat.substring(0, 8);
        }
        else if (strLng.length() > 8){
            strLng = strLng.substring(0, 8);
        }
        else if (strLat.length() > 8){
            strLat = strLat.substring(0, 8);
        }

        //拼接经纬度
        String lngAndLat = lng+","+lat;
        String strLngAndLat = strLng+","+strLat;

        //调用方法计算距离
        Double distance = getDistanceMeter(lngAndLat, strLngAndLat);
        return distance;
    }
}
